package com.ohc.leetcode.链表.repeat;

import com.ohc.leetcode.链表.K个一组翻转链表25;

import java.util.List;

/**
 * 2022年6月16日13:05:07 没搞出来，可惜 关键是要用3个变量，一个是第一个 一个是temp要保存下一个防止断线
 */
public class 反转链表02 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static ListNode reverseList(ListNode head) {
//        ListNode temp = null;
//        ListNode left = null;
//        ListNode right = head;
        ListNode left = null;
        ListNode right = head;
        ListNode temp = null;
        while (right.next != null) {
            temp = right.next;
            right.next = left;
            left = right;
            right = temp;
        }
        return right;
//        while (right != null) {
//            temp = right.next; // 保存下一个 防止断线
//            right.next = left;
//            left = right;
//            right = temp;
//        }
//        while (left != null) {
//            System.out.println("right.val = " + left.val);
//            left = left.next;
//        }
//        return null;
    }

    public static void main(String[] args) {
        ListNode listNode5 = new ListNode(5);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode1 = new ListNode(1);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        listNode5.next = null;
        reverseList(listNode1);
    }
}
